<ul>
<li><a href="#awk">awk</a></li>
<li><a href="#%e6%94%af%e6%8c%81%e6%ad%a3%e5%88%99">支持正则</a>
<ul>
<li><a href="#%e5%8f%98%e9%87%8f%e4%bd%bf%e7%94%a8%e5%92%8c%e5%88%97%e6%8e%a7%e5%88%b6">变量使用和列控制</a></li>
<li><a href="#%e5%88%97%e5%8c%b9%e9%85%8d">列匹配</a></li>
<li><a href="#%e6%ad%a3%e5%88%99%e5%8c%b9%e9%85%8d">正则匹配</a></li>
</ul></li>
<li><a href="#%e5%88%86%e9%9a%94%e7%ac%a6">分隔符</a>
<ul>
<li><a href="#%e6%9b%b4%e6%94%b9%e5%88%86%e9%9a%94%e7%ac%a6">更改分隔符</a></li>
</ul></li>
<li><a href="#begin">BEGIN</a>
<ul>
<li><a href="#%e6%97%a0%e6%96%87%e6%9c%ac%e9%a9%b1%e5%8a%a8%e8%be%93%e5%87%ba">无文本驱动输出</a></li>
</ul></li>
<li><a href="#end">END</a></li>
<li><a href="#%e6%9d%a1%e4%bb%b6%e5%92%8c%e5%be%aa%e7%8e%af">条件和循环</a></li>
<li><a href="#function">function</a>
<ul>
<li><a href="#system%22%22">system(&quot;&quot;)</a></li>
</ul></li>
<li><a href="#awk-option--f-awkscipt"><strong><code>awk [Option] -f AwkScipt</code></strong></a>
<ul>
<li><a href="#%e7%b1%bb%e6%af%94">类比</a></li>
<li><a href="#next">next</a></li>
</ul></li>
<li><a href="#view">view</a></li>
</ul>
<h1 id="awk">awk</h1>
<p><code>awk 'command' __FILE__</code></p>
<p>逐行扫描并处理</p>
<p>可以在命令中使用重定向</p>
<p><big>文本驱动</big></p>
<p><code>$0</code> 表示每一整行内容</p>
<pre class="shell"><code>$  awk &#39;{print $0}&#39; ~/.bashrc # cat ~/.bashrc
# print each line of the file</code></pre>
<h2 id="支持正则">支持正则</h2>
<table>
<thead>
<tr class="header">
<th>变量名</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ARGC</td>
<td>命令行变元个数</td>
</tr>
<tr class="even">
<td>ARGV</td>
<td>命令行变元数组</td>
</tr>
<tr class="odd">
<td>FILENAME</td>
<td>当前输入文件名</td>
</tr>
<tr class="even">
<td>FNR</td>
<td>当前文件中的记录号</td>
</tr>
<tr class="odd">
<td>FS</td>
<td>输入域分隔符，默认为一个空格</td>
</tr>
<tr class="even">
<td>RS</td>
<td>输入记录分隔符</td>
</tr>
<tr class="odd">
<td>NF</td>
<td>当前记录里域个数 number of field</td>
</tr>
<tr class="even">
<td>NR</td>
<td>到目前为止记录数 number of the current record</td>
</tr>
<tr class="odd">
<td>OFS</td>
<td>输出域分隔符</td>
</tr>
<tr class="even">
<td>ORS</td>
<td>输出记录分隔符</td>
</tr>
</tbody>
</table>
<p><code>awk 'SEARCH_MODEL [COMMAND]'__FILE__</code></p>
<p><font size=6 color=DeepSkyBlue>COMMAND:{_c_ arg}</font></p>
<p>team.txt</p>
<pre><code>Jack 1001 1976/1/2 2300
Mike 1014 1976/7/8 4300
Mary 1091 1996/1/22 3500
Mick 1234 1973/11/2 2100</code></pre>
<h3 id="变量使用和列控制">变量使用和列控制</h3>
<pre class="shell"><code>$  awk &#39;{print NR,$1,$4}&#39; team.txt #column 1 and column 4
1 Jack 2300
2 Mike 4300
3 Mary 3500
4 Mick 2100</code></pre>
<h3 id="列匹配">列匹配</h3>
<pre class="shell"><code>$  awk &#39;$2~1234&#39; team.txt # search column 2 for 1234 in team.txt
Mick 1234 1973/11/2 2100</code></pre>
<h3 id="正则匹配">正则匹配</h3>
<pre class="shell"><code>$  awk &#39;/4300/{print $1}&#39; team.txt
Mike</code></pre>
<h2 id="分隔符">分隔符</h2>
<p><code>awk [-F delimiter] 'command'__FILE__</code></p>
<p>分隔符默认为空格 该指定为输入域分隔符</p>
<pre class="shell"><code>$  awk -F: &#39;{print $1,$3}&#39; /etc/passwd</code></pre>
<h4 id="更改分隔符">更改分隔符</h4>
<pre class="shell"><code>$  awk &#39;{FS=&quot;:&quot;;OFS=&quot;?&quot;;print $1,$7}&#39; /etc/passwd | tail -n5
dnsmasq?/usr/sbin/nologin
landscape?/usr/sbin/nologin
sshd?/usr/sbin/nologin
pollinate?/bin/false
hugh?/usr/bin/zsh</code></pre>
<h2 id="begin">BEGIN</h2>
<p>最先执行 <code>BEGIN{}</code></p>
<p>给 team.txt 显示时加文件头</p>
<pre class="shell"><code>$  awk &#39;BEGIN{print &quot;Name Number Birth Wage&quot;}{print $0}&#39; team.txt #转义字符可以用
Name Number Birth Wage
Jack 1001 1976/1/2 2300
Mike 1014 1976/7/8 4300
Mary 1091 1996/1/22 3500
Mick 1234 1973/11/2 2100</code></pre>
<h3 id="无文本驱动输出">无文本驱动输出</h3>
<pre class="shell"><code>$  awk &#39;BEGIN{print &quot;Hello World&quot;}&#39;</code></pre>
<h2 id="end">END</h2>
<p>最后执行 <code>END{}</code></p>
<p>计算平均工资</p>
<pre class="shell"><code>$  awk &#39;{sum+=$4}END{print sum/NR}&#39; team.txt
3050</code></pre>
<h2 id="条件和循环">条件和循环</h2>
<pre class="shell"><code>$  awk &#39;{if($4&gt;3000){print $0}}&#39; team.txt
Mike 1014 1976/7/8 4300
Mary 1091 1996/1/22 3500
$  awk &#39;{while($4&gt;3000){print $0;exit}}&#39; team.txt
Mike 1014 1976/7/8 4300</code></pre>
<h2 id="function">function</h2>
<p><big><a href="https://www.w3cschool.cn/awk/3ja71k8i.html">内置函数</a></big></p>
<div class="figure">
<img src="https://gitee.com/HughNew/Photos/raw/master/Shell/awk_math_function.png" alt="math" />
<p class="caption">math</p>
</div>
<p><font size=6 color=#A05653>Index begin with 1</font></p>
<table>
<thead>
<tr class="header">
<th>函数</th>
<th>示例</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>index</td>
<td><code>index(&quot;HelloWorld&quot;,&quot;oWo&quot;)</code> # <strong>return o if npos</strong></td>
</tr>
<tr class="even">
<td>sbustr</td>
<td><code>substr(&quot;011-234-117&quot;,1,4)</code> # <strong>substr(str,begin,length)</strong></td>
</tr>
<tr class="odd">
<td>length</td>
<td><code>length(&quot;HelloWorld&quot;)</code></td>
</tr>
</tbody>
</table>
<h3 id="system">system(&quot;&quot;)</h3>
<h2 id="awk-option--f-awkscipt"><strong><code>awk [Option] -f AwkScipt</code></strong></h2>
<p>awk 函数与 bash 相近</p>
<div class="sourceCode"><pre class="sourceCode awk"><code class="sourceCode awk"><span class="kw">function</span> __FuncName(_arglist)<span class="kw">{</span>
 <span class="co"># function body</span>
<span class="kw">}</span></code></pre></div>
<h3 id="类比">类比</h3>
<p>流程控制与C基本相同</p>
<p>printf 与C相同</p>
<p><code>printf [-v var] “” ArgList</code></p>
<p>-v 存入变量中</p>
<p>if/else while break continue exit [结束当前,不跳过 END 块]</p>
<h3 id="next">next</h3>
<p>停止处理当前记录，并且进入到下一条记录的处理过程</p>
<pre class="shell"><code>$  awk &#39;{if(/4300/)next;print $0}&#39; team.txt #匹配到 4300 则不输出
Jack 1001 1976/1/2 2300
Mary 1091 1996/1/22 3500
Mick 1234 1973/11/2 2100</code></pre>
<p>变量使用不需要声明</p>
<pre class="shell"><code>$  awk &#39;BEGIN{a=42;b=3;b^=2;print a,b}&#39;
# ^ 为乘方</code></pre>
<h2 id="view">view</h2>
<p><img src="https://gitee.com/HughNew/Photos/raw/master/Shell/awk.png" alt="view" /> <!--![View](./awk.png)--></p>
